<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="search-container">
        <el-date-picker
          v-model="select_month"
          type="month"
          placeholder="选择月或周"
          style="width: 120px;margin-right:1rem;"
          :clearable="false"
          @change="handleSelectMonth"
        />
        <el-button class="filter-item" type="primary" icon="el-icon-search" style="margin-right:1rem;" @click="handleFilter">
          查询
        </el-button>
      </div>
      <!-- <pagination v-show="total>0" :total="total" :page.sync="searchQuery.pageNo" :layout="'total, sizes, prev, pager, next, jumper'" :limit.sync="searchQuery.pageSize" @pagination="getPerformanceDatas" /> -->
    </div>

    <el-table
      :key="tableKey"
      v-loading="pageLoading"
      :data="listData"
      border
      fit
      style="width: 100%;"
    >
      <el-table-column label="名次" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ rankList.indexOf(row.score) + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成员" align="center">
        <template slot-scope="{row}">
          <span>{{ row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门" align="center">
        <template slot-scope="{row}">
          <span>{{ row.organization }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绩效得分" align="center">
        <template slot-scope="{row}">
          <span>{{ row.score }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { dealWithMonthDay } from '@/utils/index'
// import Pagination from '@/components/Pagination'
import * as PerformanceApi from '@/api/performance'

const dateMonth = new Date()
dateMonth.setMonth(dateMonth.getMonth())
dateMonth.setDate(1)

export default {
  name: 'Summary',
  // components: { Pagination },
  data() {
    return {
      tableKey: 0,
      total: 0,
      select_month: dateMonth,
      searchQuery: {
        pageNo: 1,
        pageSize: 50,
        start_time: '',
        end_time: ''
      },
      pageLoading: false,
      listData: [],
      rankList: []
    }
  },
  mounted() {
    this.handleSelectMonth(this.select_month)
  },
  methods: {
    async getPerformanceDatas(pageQuery) {
      if (pageQuery) {
        this.searchQuery = Object.assign(this.searchQuery, pageQuery)
      }
      this.listData = []
      this.rankList = []
      this.pageLoading = true
      const { data } = await PerformanceApi.getPerformanceRank(this.searchQuery)
      if (data && data.total) {
        data.items.map(v => {
          this.rankList.push(v.score)
        })
        // this.rankList = new Array(new Set(...this.rankList))
        // this.rankList = this.rankList.sort((a, b) => a - b)

        this.listData = data.items
        this.total = data.total
      }
      this.pageLoading = false
    },
    handleFilter() {
      this.searchQuery.pageNo = 1
      this.getPerformanceDatas()
    },
    handleSelectMonth(val) {
      const { startTime, endTime } = dealWithMonthDay(val)
      this.searchQuery['start_time'] = startTime
      this.searchQuery['end_time'] = endTime
      this.getPerformanceDatas()
    }
  }
}
</script>

<style scoped>
.filter-container {
  display: flex;
  justify-content: space-between;
}
.pagination {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
}
.search-container {
  display: flex;
  align-items: flex-start;
}
</style>
